<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>permutation</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        function permutationStr(per) {
            if (per.length === 1) {
                return [per];
            }
            let res = [];
            [...per].forEach((v, i, str) => {
                const temp = [...str];
                const header = temp.splice(i, 1);
                permutation(temp).forEach(value => res.push([header, ...value].join('')));
            });
            return [...new Set(res)];
        }
        function permutationArr(per) {
            if (per.length === 1) {
                return [per];
            }
            let res = [];
            per.forEach((v, i, arr) => {
                let temp = [...arr];
                let header = temp.splice(i, 1);
                permutation(temp).forEach(val => res.push([...header, ...val]));
            });
            return [...new Set(res.map(value => JSON.stringify(value)))].map(val =>
                JSON.parse(val)
            );
            // 判断相同数组并去重，将数组转为JSON字符串，然后进行字符串去重,该方法无法进行数据类型判断，会将 1和'1'判断为相等，所以有相关数据的话不可以使用
        }
        console.log(permutationStr('abca'));
        console.log(permutationArr([1, 2, 3, 2]));
    </script>
</body>

</html>